import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    plugins: [react()],
    build: {
      terserOptions: {
        compress: {
          drop_console: true, // 清除console.log
        },
      },
    },
    resolve: {
      alias: [
        { find: "@", replacement: path.join(__dirname, "src") },
        { find: /^~/, replacement: "" },
      ],
    },
    css: {
      // css预处理器
      preprocessorOptions: {
        less: {
          charset: false,
          additionalData: '@import "./src/global.less";',
          javascriptEnabled: true,
        },
      },
    },
    base: "./", // 配置公共路径
    server: {
      // 代理请求 yarn start --debug 调试代理
      host: "0.0.0.0",
      port: 3000,
      proxy: {
        [env["VITE_API_DRWA_PREFIX"]]: {
          target: env["VITE_API_DRWA_URL"],
          changeOrigin: true,
          rewrite: (path) => path.replace(env["VITE_API_DRWA_PREFIX"], ""),
        },
        [env["VITE_API_IMG_PREFIX"]]: {
          target: env["VITE_API_IMG_URL"],
          changeOrigin: true,
          rewrite: (path) => path.replace(env["VITE_API_IMG_PREFIX"], ""),
        },
      },
    },
  };
});
